//
//  BCWalletExchangeFromToPickerView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/22.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCWalletExchangeFromToPickerView.h"

#import "BCWalletExchangeDataModel.h"

@interface BCWalletExchangeFromToPickerView () <UIPickerViewDataSource,UIPickerViewDelegate>


/**
 *dataSource
 */
@property (nonatomic, weak) NSArray *data;

@end

@implementation BCWalletExchangeFromToPickerView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
- (void)setPickSource:(BCWalletExchangeDataModel *)pickSource {
    _pickSource = pickSource;
    if (pickSource) {
        self.data = pickSource.exchangeData.data;
        self.dataSource = self;
        self.delegate = self;
    }else {
        self.dataSource = nil;
        self.delegate = nil;
    }
    [self reloadAllComponents];
    //初始化选中
    if (pickSource) {
        UserWalletExchangeDataCurrencyModel *from = pickSource.fromCurrency;
        UserWalletExchangeDataCurrencyModel *to = pickSource.toCurrency;
        //
        if (from) {
            for (int i=0; i<self.data.count; i++) {
                UserWalletExchangeDataCurrencyModel *model = self.data[i];
                if ([from.cur_id isEqualToString:model.cur_id]) {
                    [self selectRow:i inComponent:1 animated:false];
                    break;
                }
            }
        }
        if (to) {
            for (int i=0; i<self.data.count; i++) {
                UserWalletExchangeDataCurrencyModel *model = self.data[i];
                if ([to.cur_id isEqualToString:model.cur_id]) {
                    [self selectRow:i inComponent:3 animated:false];
                    break;
                }
            }
        }
    }
}

#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (self.pickSource) {
        return 4;
    }
    return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (self.pickSource) {
        switch (component) {
            case 0:
                return 1;
                break;
            case 1:
                return self.data.count;
                break;
            case 2:
                return 1;
                break;
            case 3:
                return self.data.count;
                break;
            default:
                break;
        }
    }
    return 0;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (self.pickSource) {
        switch (component) {
            case 0://from
            {
                NSString *fromString = self.pickSource.exchangeData.flow.from;
                return [[NSAttributedString alloc] initWithString:fromString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:5 weight:UIFontWeightRegular],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#9B9B9B"]}];
            }
                break;
            case 1://source
            {
                UserWalletExchangeDataCurrencyModel *model = self.data[row];
                NSString *title = model.acronyms;
                return [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15 weight:UIFontWeightRegular],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#333333"]}];
            }
                break;
            case 2://to
            {
                NSString *toString = self.pickSource.exchangeData.flow.reach;
                return [[NSAttributedString alloc] initWithString:toString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:5 weight:UIFontWeightRegular],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#9B9B9B"]}];
            }
                break;
            case 3://source
            {
                UserWalletExchangeDataCurrencyModel *model = self.data[row];
                NSString *title = model.acronyms;
                return [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15 weight:UIFontWeightRegular],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#333333"]}];
            }
                break;
            default:
                break;
        }
    }
    return nil;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44.0f;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //1,3
    UserWalletExchangeDataCurrencyModel *from = self.data[[pickerView selectedRowInComponent:1]];
    UserWalletExchangeDataCurrencyModel *to = self.data[[pickerView selectedRowInComponent:3]];
    BOOL theSame = false;
    if ([from.cur_id isEqualToString:to.cur_id]) {
        theSame = true;
    }else {
        
    }
    if (self.chooseSame) {
        self.chooseSame(theSame);
    }
}

- (UserWalletExchangeDataCurrencyModel *)from {
    UserWalletExchangeDataCurrencyModel *from = self.data[[self selectedRowInComponent:1]];
    return from;
}
- (UserWalletExchangeDataCurrencyModel *)to {
    UserWalletExchangeDataCurrencyModel *to1 = self.data[[self selectedRowInComponent:3]];
    return to1;
}


#pragma mark - override

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGFloat x = point.x;
    CGFloat width = self.frame.size.width;
    if (x<=width/4) {
        return nil;
    }
    if (x>=width/2&&x<=(width*.75)) {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}


@end
